package com.hospital.servlet;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class Show extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/xml;charset=UTF-8");
		PrintWriter out=response.getWriter();
		out.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		out.println("<infoPic><piclist>");
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://localhost/hospital?useUnicode=true&characterEncoding=UTF-8","root","123456");
			String sql = "SELECT * FROM news WHERE picnews=? AND type=? ORDER BY pubdate DESC LIMIT 0,5";
			pstmt = conn.prepareStatement(sql);
			pstmt.setBoolean(1, true);
			pstmt.setString(2, "xwzx");
			rs = pstmt.executeQuery();
			while(rs.next()) {
				out.println("<pic><url>" 
								+ rs.getString("imgdir") 
								+ "</url><title>" 
								+ rs.getString("title") 
								+ "</title><link>"
								+ request.getContextPath()
								+ "/shownews?nid=" 
								+ rs.getInt("nid") 
								+ "&type=" 
								+ rs.getString("type") 
								+ "</link></pic>");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				rs.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			try {
				pstmt.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			try {
				conn.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		//out.println("<pic><url>ueditor/jsp//upload/20130414/2491365882091940.jpg</url><title>这里有着最好的医疗设备1112</title><link>http://www.lvshu.com</link></pic>");


		//out.println("<pic><url>img/img2.jpg</url><title>这里有着最好的医疗设备2121322</title><link>http://www.lvshu.com</link></pic>");


		//out.println("<pic><url>img/img3.jpg</url><title>这里有着最好的医疗设备fdsfdsf222</title><link>http://www.lvshu.com</link></pic>");


		//out.println("<pic><url>img/img4.jpg</url><title>这里有着最好的医疗设备222</title><link>http://www.lvshu.com</link></pic>");
		out.println("</piclist></infoPic>");
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doGet(request, response);
	}

}
